﻿using Microsoft.AspNetCore.Mvc;
using MyBlog2.Repository;
using MyBlog2.Models;
using Microsoft.AspNetCore.Authorization;

namespace MyBlog2.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Authorize]
    public class ArticleClassifyManageController : Controller
    {
        private readonly IArticleClassifyRepository _articleClassifyRepository;

        public ArticleClassifyManageController(IArticleClassifyRepository articleClassifyRepository)
        {
            this._articleClassifyRepository = articleClassifyRepository;
        }


        /// <summary>
        /// 分类管理
        /// </summary>
        /// <returns></returns>
        [Route("Admin/ArticleClassifyManage/ArticleClassifyManage", Name = "ArticleClassifyManage")]
        public IActionResult ArticleClassifyManage()
        {
            var list = _articleClassifyRepository.GetArticleClassifieList();
            return View(list);
        }

        /// <summary>
        /// 分类添加页面
        /// </summary>
        /// <returns></returns>
        [Route("Admin/ArticleClassifyManage/AddClassify", Name = "AddClassify")]
        public IActionResult AddClassify(ArticleClassify addClassify)
        {
            return View(addClassify);
        }

        /// <summary>
        /// 分类添加提交
        /// </summary>
        /// <param name="classify"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Admin/ArticleClassifyManage/AddClassify", Name = "AddClassifyPost")]
        [ValidateAntiForgeryToken]
        public IActionResult AddClassify([Bind("Name,Sort")] ArticleClassify classify,string nouse)
        {
            if (ModelState.IsValid)     //验证通过
            {
                var cls = _articleClassifyRepository.GetOneArticleClassify(classify.Id);
                if (classify != null)   //分类已存在
                {
                    return BadRequest("分类已存在！");
                }
                var c = _articleClassifyRepository.AddArticleClassify(classify);
                if (c == null)
                {
                    return BadRequest("分类添加失败！");
                } 
                return RedirectToRoute("ArticleClassifyManage");
            }
            else
            {
                return BadRequest("数据填写不正确！");
            }
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <returns></returns>
        [Route("Admin/ArticleClassifyManage/DeleteClassify", Name = "DeleteClassify")]
        public IActionResult DeleteClassify(int id)
        {
            if (_articleClassifyRepository.DeleteArticleClassify(id))   //删除成功
            {
                return Redirect("/Admin/ArticleClassifyManage/ArticleClassifyManage");
            }
            else
            {
                return BadRequest("删除失败！");
            }
            
        }

        /// <summary>
        /// 分类编辑页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Admin/ArticleClassifyManage/EditClassify", Name = "EditClassify")]
        public IActionResult EditClassify(int id)
        {
            var c = _articleClassifyRepository.GetOneArticleClassify(id);
            return View(c);
        }

        /// <summary>
        /// 分类编辑提交
        /// </summary>
        /// <param name="classify"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Admin/ArticleClassifyManage/EditClassify", Name = "EditClassifyPost")]
        public IActionResult EditClassify(ArticleClassify classify)
        {
            if (ModelState.IsValid)
            {
                var c = _articleClassifyRepository.EditArticleClassify(classify);
                if (c != null)
                {
                    return Redirect("/Admin/ArticleClassifyManage/ArticleClassifyManage");
                }
                else
                {
                    return BadRequest("编辑失败！");
                }
            }
            else
            {
                return BadRequest("数据填写不正确！");
            }
        }
    }
}